package main

import (
	"fmt"
	"strings"
)

func mostCommonWord(paragraph string, banned []string) string {
	countMap := make(map[string]int)
	bannedMap := make(map[string]bool)
	for _, ban := range banned {
		bannedMap[ban] = true
	}

	index := -1
	isWord := false
	for i := 0; i < len(paragraph); i++ {
		if isLetter(paragraph[i]) {
			if !isWord {
				isWord = true
				index = i
			}
		} else {
			if isWord {
				isWord = false
				word := paragraph[index:i]
				countMap[strings.ToLower(word)]++
			}
		}
	}
	if isWord {
		word := paragraph[index:]
		countMap[strings.ToLower(word)]++
	}
	//fmt.Println(countMap)
	res := ""
	maxCount := 0
	for k, v := range countMap {
		if v > maxCount && !bannedMap[k] {
			maxCount = v
			res = k
		}
	}
	return res
}

func isLetter(ch byte) bool {
	return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}

func main() {
	s := "Bob. hIt, baLl"
	banned := []string{"bob", "hit"}

	word := mostCommonWord(s, banned)
	fmt.Println(word)
}
